package com.online.onlineshop.controllers;


import com.online.onlineshop.entity.Address;
import com.online.onlineshop.services.ForAddressService;
import com.online.onlineshop.utils.ResultForFrontEnd;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RequestMapping("/address")
@RestController
public class AddressController extends RootController {
    @Autowired
    private ForAddressService addressService;

    @RequestMapping("/putNewAddress")
    public ResultForFrontEnd<Address> putNewAddress(Address address,
                                                    HttpSession httpSession) {
        int uid = getUidFromSession(httpSession);
        String userName = getUserNameFromSession(httpSession);

        addressService.putNewAddress(uid, userName, address);

        return new ResultForFrontEnd<>(ok);

    }

    @RequestMapping({"", "/"})
    public ResultForFrontEnd<List<Address>> getAddresses(HttpSession session
    ) {
        int uid = getUidFromSession(session);
        List<Address> data = addressService.getAddressByUid(uid);
        System.out.println(data);
        return new ResultForFrontEnd<>(ok, data);

    }

    @RequestMapping("{aid}/deleteAddress")
    public ResultForFrontEnd<Void> deleteAddress(@PathVariable("aid") Integer aid, HttpSession session) {
        int uid = getUidFromSession(session);
        addressService.deleteAddress(aid, uid);
        return new ResultForFrontEnd<>(ok);
    }


}
